package com.sy.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sy.dto.LeaveSimpleMessage;
import com.sy.dto.LeaveSimpleMessageTeacher;
import com.sy.entity.LeaveMessageDetail;
import com.sy.service.NewLeavenWordsService;
import com.sy.utils.EmojiConverterUtil;
import com.sy.vo.NewLeavenWordsVo;
import com.sy.vo.NewReplyWordsVo;
import com.sy.vo.SearchLeaWordByKeyWordsVo;
import com.sy.vo.SearchTeaLeaWordByKeyVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;

import java.util.List;

import static org.springframework.http.HttpStatus.BAD_REQUEST;

//添加留言
@Service
public class NewLeavenWordsServiceImpl extends BaseServiceImpl implements NewLeavenWordsService {
    @Override
    public boolean addNewMessage(NewLeavenWordsVo newLeavenWordsVo) {
        //校验记得做
        String content = newLeavenWordsVo.getContent();
        String studentId = newLeavenWordsVo.getStudentId();
        String teacherId = newLeavenWordsVo.getTeacherId();

        if (StringUtils.isBlank(content)){
            buildError(BAD_REQUEST,"留言内容不能为空！");
        }
        if(StringUtils.isBlank(studentId)){
            buildError(BAD_REQUEST,"学生id不能为空！");
        }
        if(StringUtils.isBlank(teacherId)){
            buildError(BAD_REQUEST,"老师id不能为空！");
        }
        String beforeContent = newLeavenWordsVo.getContent();
        String afterContent = EmojiConverterUtil.emojiConvert1(beforeContent);
        newLeavenWordsVo.setContent(afterContent);
        return newLeavenWordsMapper.addNewLeavenWords(newLeavenWordsVo);
    }

    @Override
    public boolean addNewReplyWords(NewReplyWordsVo newReplyWordsVo) {
        String content = newReplyWordsVo.getContent();
        String leaveMessageId = newReplyWordsVo.getLeaveMessageId();
        String name = newReplyWordsVo.getName();

        if (StringUtils.isBlank(content)){
            buildError(BAD_REQUEST,"留言内容不能为空！");
        }
        if(StringUtils.isBlank(leaveMessageId)){
            buildError(BAD_REQUEST,"留言id不能为空！");
        }
        if(StringUtils.isBlank(name)){
            buildError(BAD_REQUEST,"回复姓名不能为空！");
        }
        String beforeContent = newReplyWordsVo.getContent();
        String afterContent = EmojiConverterUtil.emojiConvert1(beforeContent);
        newReplyWordsVo.setContent(afterContent);
        return newLeavenWordsMapper.addNewReplyWords(newReplyWordsVo);
    }

    @Override
    public boolean deleteLeavenWord(String id) {
        if (StringUtils.isBlank(id)) {
            buildError(BAD_REQUEST,"留言id不能为空！");
        }
        List<LeaveMessageDetail> allReplyMessageById = newLeavenWordsMapper.getAllReplyMessageById(id);
        for (LeaveMessageDetail leaveMessageDetail : allReplyMessageById) {
            Integer leaveMessageDetailId = leaveMessageDetail.getId();
            newLeavenWordsMapper.deleteReplyWordById(leaveMessageDetailId);
        }
        return newLeavenWordsMapper.deleteLeavenWordById(id);
    }

    @Override
    public Page<LeaveSimpleMessage> getAllLeaveMessage(SearchLeaWordByKeyWordsVo vo) {
        PageHelper.startPage(vo.getPage(), vo.getLimit());
        Page<LeaveSimpleMessage> pageResult = (Page<LeaveSimpleMessage>) newLeavenWordsMapper.getAllLeaveMessageByStuId(vo);
        for (LeaveSimpleMessage leaveSimpleMessage : pageResult) {
            String beforeContent = leaveSimpleMessage.getContent();
            String afterContent = EmojiConverterUtil.emojiRecovery2(beforeContent);
            leaveSimpleMessage.setContent(afterContent);
        }
        return pageResult;
    }

    @Override
    public Page<LeaveSimpleMessageTeacher> getAllLeaveMessageByTeacher(SearchTeaLeaWordByKeyVo vo) {
        PageHelper.startPage(vo.getPage(), vo.getLimit());
        Page<LeaveSimpleMessageTeacher> pageResult = (Page<LeaveSimpleMessageTeacher>) newLeavenWordsMapper.getAllLeaveMessageByTeaId(vo);
        for (LeaveSimpleMessageTeacher leaveSimpleMessageTeacher : pageResult) {
            String beforeContent = leaveSimpleMessageTeacher.getContent();
            String afterContent = EmojiConverterUtil.emojiRecovery2(beforeContent);
            leaveSimpleMessageTeacher.setContent(afterContent);
        }
        return pageResult;
    }

    @Override
    public List<LeaveMessageDetail> getAllReplyMessage(String leaveMessageId) {
        List<LeaveMessageDetail> allReplyMessageById = newLeavenWordsMapper.getAllReplyMessageById(leaveMessageId);
        for (LeaveMessageDetail leaveMessageDetail : allReplyMessageById) {
            String beforeContent = leaveMessageDetail.getContent();
            String afterContent = EmojiConverterUtil.emojiRecovery2(beforeContent);
            leaveMessageDetail.setContent(afterContent);
        }
        return allReplyMessageById;
    }
}
